package mango.server;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.util.*;


/**
 * @author danielsmw
 *
 */
public class MangoServer extends Thread {

	public final static int DEFAULT_PORT = 4664;
	public int port;
	public ServerSocket listenSocket;
	public ThreadGroup threadGroup;
	public java.awt.List connectionList;
	public Vector connections;
	public Vulture vulture;
	public Gateway SaintPeter;
	private Hashtable<String, Integer> bin = new Hashtable<String, Integer>();
	
	public MangoServer (int p) {
		super("MangoServer");
		if (p==0)
			p = DEFAULT_PORT;
		port = p;
		
		try {
			listenSocket = new ServerSocket(port);
		}
		catch (IOException e) {
			System.err.println("<!> Cannot create server socket: "+e);
			System.exit(-1);
		}
		threadGroup = new ThreadGroup("Server Connections");
		
		//Frame f = new Frame("Server Status");
		connectionList = new java.awt.List();
		//f.add("Center", connectionList);
		//f.resize(400,200);
		//f.show();
		
		mango.server.database.Database database = new mango.server.database.Database(this);
		
		connections = new Vector();
		vulture = new Vulture(this);
		SaintPeter = new Gateway(listenSocket, threadGroup, vulture, connectionList, connections, this);
		this.start();
	}
	
	public void run() {
		initializeCommands(bin);
		Scanner stdin = new Scanner(System.in);
		System.out.println("Java MangoServer console");
		System.out.println("(c) 2007 AMHSAPCSAB");
		System.out.println("Type 'help' for a list of commands.\n");
		int commandNumber=0;
		while (true) {
			commandNumber++;
			System.out.print(commandNumber+" > ");
			String input = stdin.nextLine();
			String[] parts = input.split("[ ]");
			
			if (bin.containsKey(parts[0])) {
				switch (bin.get(parts[0])) {
					case 1: printHelp(); break;
					case 2: safeShutdown(); break;
					case 3: who(); break;
				}
			}
			
			
		}
		
	}
	
	public void printHelp() {
		System.out.println("Available Commands");
		System.out.println("------------------");
		System.out.println("help		Displays this message");
		System.out.println("shutdown	Kindly boots users and quits server.");
		System.out.println("who			Enumerates active clients.");
	}
	
	public void println (String str) {
		System.out.println(str);
	}
	
	public void pushToAllClients(String message) {
		Thread[] threads = new Thread[threadGroup.activeCount()];
		threadGroup.enumerate(threads);
		for (int j=0; j<threads.length; j++) {
			try {
				((Connection)threads[j]).pushToClient("message");
			}
			catch (Exception e) {};
		}
	}
	
	public void safeShutdown() {
		pushToAllClients("<$> Disconnect Signal");
		System.out.println("Exiting.\n");
		System.exit(1);
	}
	
	public void who() {
		System.out.println("Active Clients:");
		String[] arr = connectionList.getItems();
		for (int k=0; k<arr.length; k++) {
			System.out.println(arr[k]);
		}
	}
	
	public void initializeCommands (Hashtable h) {
		h.put("help",1);
		h.put("shutdown", 2);
		h.put("who", 3);
	}
	
	public static void main(String[] args) {
		int port = 0;
		if (args.length == 1) {
			try { port = Integer.parseInt(args[0]); }
			catch (NumberFormatException e) { port = 0; }
		}
		new MangoServer(port);
	}

	
}
